﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Data;
using DBHelper;

//网站评分系统
namespace Http
{
    /// <summary>
    /// 网站综合权值计算类
    /// </summary>
    public partial class HttpRequesst
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public HttpRequesst()
        {
            string getKeyword = "select * from t_keyword";
            DataTable dt = new DataTable();
            using (DBase db = new DBase())
            {
                dt = db.GetDataTable(getKeyword);
            }
            if (dt != null && dt.Rows.Count != 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    keyword.Add(dt.Rows[i][0].ToString());
                }
            }
        }

        /// <summary>
        /// 传递域名自动评分
        /// </summary>
        /// <param name="domain">域名</param>
        public void Requesst(string domain)
        {
            _colligate.Domain = domain;

            RequestDomain(domain);  //请求网站
        }

        /// <summary>
        /// 评分完成时执行的事件
        /// </summary>
        public event On_GradeSucess On_Sucecc;

        /// <summary>
        /// 评分结果
        /// </summary>
        private Colligate _colligate;

        /// <summary>
        /// 请求网站
        /// </summary>
        /// <param name="domain">域名</param>
        private void RequestDomain(string domain)
        {
            DateTime start = DateTime.Now;  //开始时间
            string content = string.Empty;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(domain);
                request.Method = "GET";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727)";
                request.ContentType = "application/x-www-form-urlencoded";
                request.Referer = domain;
                request.CookieContainer = new CookieContainer();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader read = new StreamReader(response.GetResponseStream(), Encoding.Default);

                DateTime end = DateTime.Now;    //结束时间
                TimeSpan span = end - start;
                content = read.ReadToEnd();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    _colligate.HttpCode = 200;
                }
                else
                {
                    _colligate.HttpCode = 0;
                }
                double spanLength = 97515 / 50;
                _colligate.Speed = 5 - (span.TotalMilliseconds / spanLength / 10);             //响应时间(5分制)

                spanLength = 592742 / 50;
                double contentLength = Convert.ToDouble(response.ContentLength);
                if (contentLength > 0)
                {
                    _colligate.PageSize = contentLength / spanLength / 10;              //内容长度(5分制)
                }
                else
                {
                    _colligate.PageSize = contentLength;
                }

                _colligate.Content = ColligateContent(content);                         //内容中关键字匹配次数
                _colligate.GoogleEmbody = GetGoogleEmbody(domain);                      //Google收录
                _colligate.GooglePR = GetGooglePR(domain);                              //Google PR
                _colligate.BaiduEmbody = GetBaiduEmbody(domain);                        //baidu 收录
                _colligate.AbetHtml = 1;                                                //默认支持文本
                _colligate.Destoon = GetDestoonScore(content);                          //检测destoon网站,分值越大相似度越高

                read.Close();
                response.Close();
            }
            catch { }
            finally
            {
                On_Sucecc(_colligate);  //执行评分完成事件
            }
        }
    }
}